package com.itheima.service;

import com.itheima.dto.ReviewDTO;
import com.itheima.entity.Review;
import com.itheima.repository.ReviewRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ReviewService {

    @Autowired
    private ReviewRepository reviewRepository;

    // 添加审核记录
    public Review createReview(Review review) {
        review.setReviewedAt(new Date());
        return reviewRepository.save(review);
    }

    // 获取所有审核记录
    public List<Review> getAllReviews() {
        return reviewRepository.findAll();
    }

    // 根据动物 ID 获取审核记录
    public List<Review> getReviewsByAnimalId(Long animalId) {
        return reviewRepository.findByAnimalId(animalId);
    }



    // 更新审核记录
    public Review updateReview(Long reviewId, ReviewDTO reviewDTO) {
      Review review = reviewRepository.findById(reviewId).get();
        if(reviewDTO.getAnimal_id()!=null)
        {
            review.setAnimalId(reviewDTO.getAnimal_id());
        }
        if(reviewDTO.getReviewComment()!=null)
        {
            review.setReviewComment(reviewDTO.getReviewComment());
        }
        if(reviewDTO.getReviewStatus()!=null)
        {
            review.setReviewStatus(reviewDTO.getReviewStatus());
        }
       review.setReviewedAt(new Date());
        return reviewRepository.save(review);
    }

    // 删除审核记录
    public void deleteReview(Long reviewId) {
        if (reviewRepository.existsById(reviewId)) {
            reviewRepository.deleteById(reviewId);
        } else {
            throw new RuntimeException("Review not found with ID: " + reviewId);
        }
    }
}